<?php

namespace app\controllers\admin\uni\finance;

use app\controllers\admin\base\AdminController;
use app\controllers\pay\ForController;
use app\models\SetUserUp;
use app\models\system\SetApp;
use app\models\user\UserAccount;
use app\models\user\UserCash as Model;
use app\models\user\UserCommission;

class CashController extends AdminController
{
    public function actionList()
    {
        $sn = $this->post('sn');
        $user_id = $this->post('user_id');
        $export = $this->post('export');

        $start = $this->post('start');
        $end = $this->post('end');

        $mobile = $this->post('mobile');
        $nickname = $this->post('nickname');

        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $sort = $this->post('sort');

        $query = Model::find();

        if ($sort) {
            $prop = substr($sort, 0, 1);
            $prop = $prop == '-' ? SORT_DESC : SORT_ASC;
            $order = substr($sort, 1);
            $query->orderBy([$order => $prop]);
        } else {
            $query->orderBy(['id' => SORT_DESC]);
        }

        if ($user_id) {
            $query->andWhere(['user_id' => $user_id]);
        }
        if ($sn) {
            $query->andWhere(['like', 'sn', $sn]);
        }
        if ($mobile) {
            $query->andWhere(['like', 'mobile', $mobile]);
        }
        if ($nickname) {
            $query->andWhere(['like', 'nickname', $nickname]);
        }

        if ($start) {
            $query->andWhere(['>=', 'create_time', $start]);
        }
        if ($end) {
            $query->andWhere(['<=', 'create_time', $end]);
        }

        // export
        if ($export) {
            return $this->success($query->asArray()->all());
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->limit($page_size)
            ->asArray()
            ->all();

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);

    }

    public function actionPayFor()
    {
        $id = $this->post('id');
        $model = Model::findOne(['id' => $id]);

        if (!$model) {
            return $this->fail('操作错误');
        }

        if ($model->status == 1) {
            return $this->fail('不要重复支付');
        }

        if ($model->amount <= 0) {
            return $this->fail('提现金额错误');
        }

        $set = SetApp::getLastOne();
        // var_dump($set->third_pay);exit;

        // 送积分
        // var_dump($model->service);exit;
        // $account = UserAccount::findOne(['user_id' => $model->user_id]);
        // $account->score += $model->service * 10;
        // $account->save();

        if ($set->third_pay == 0) {
            // 送积分
            $this->saveScore($model->user_id, $model->service * 10);

            // manual ops
            $model->status = 1;
            $model->save();
            return $this->success();
        }

        // manual ops
        // $model->status = 1;
        // $model->save();
        // return $this->success();


        // pay for

        try {
            $result = ForController::pay(
                $model->sn,
                $model->user_id,
                ($model->amount - $model->service),
                $model->bank_number,
                $model->nickname,
                1,
                '提现'
            );
            if ($result) {

                // 送积分
                $this->saveScore($model->user_id, $model->service * 10);

                $model->status = 1;
                $model->save();

                return $this->success();
            }
        } catch (\Exception $e) {
            var_dump($e->getMessage());exit;
        }

        return $this->fail('请求支付失败');

    }

    public function actionReject()
    {
        $id = $this->post('id');
        $txt = $this->post('txt');

        $model = Model::findOne(['id' => $id]);
        if (!$model || $model->status == -1) {
            return $this->fail('操作错误');
        }

        $model->status = -1;
        $model->reject = $txt;
        $model->save();

        $account = UserAccount::findOne(['user_id' => $model->user_id]);
        $account->balance += $model->amount;
        $account->save();

        return $this->success();
    }

    protected function saveScore($user_id, $score)
    {
        if ($score >= 0.01) {
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = 0;
            $model->order_id = 0;
            $model->score = $score;
            $model->status = 1;
            $model->award_name = '提现送积分';
            $model->award_type = 1;
            $model->save();

            $account = UserAccount::findOne(['user_id' => $user_id]);
            if ($account) {
                $account->score += $score;
                $account->save();
            }
        }
    }
}